package OperationSystem.day1206.OutputStream;

import java.io.*;

public class CopyFile {
    public static void main(String[] args) throws IOException {
        String srcPath = "00000122.jpg";
        String destPath = "dest.jpg";

        //1、判断源文件是否存在
        File srcFile = new File(srcPath);
        if (!srcFile.exists()) {
            System.out.println("源文件不存在," + srcPath);
            return;
        }
        if (srcFile.isDirectory()) {
            System.out.println("源文件是目录," + srcPath);
            return;
        }

        //2、判断目标文件
        File destFile = new File(destPath);
        if (destFile.exists()) {
            System.out.println("目标文件已存在," + destPath);
            return;
        }

        //3、准备复制-----以二进制的形式进行复制
        //一次读4K数据，把4K数据写入到目标中
        try (InputStream is = new FileInputStream(srcFile)) {
            try (OutputStream os = new FileOutputStream(destFile)) {
                byte[] buffer = new byte[4096];   //4K字节

                int len;
                while ((len = is.read(buffer, 0, 4096)) != -1) {
                    //buffer[0,len)
                    os.write(buffer, 0, len);
                }
                os.flush();
            }
        }
    }
}
